package com.hp.bon.sgw.wh.utils;

import java.io.StringReader;

import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.events.XMLEvent;

import org.apache.commons.lang.StringUtils;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.STAXEventReader;

public class XmlUtils {

	private static DefaultXMLEventFilter xmlEventFilter = new DefaultXMLEventFilter();

	static XMLInputFactory inputFactory = XMLInputFactory.newInstance();

	/**
	 * 利用StAXParser来解析XML，提高性能和效率
	 */
	public static Document parseText(String xmlContent) throws Exception {
		XMLEventReader xmlEventReader = inputFactory.createFilteredReader(inputFactory.createXMLEventReader(new StringReader(xmlContent)), xmlEventFilter);
		STAXEventReader reader = new STAXEventReader();
		Document result = reader.readDocument(xmlEventReader);
		return result;
	}
	
	/**
	 * 获取单节点值
	 * @param root
	 * @param path
	 * @return
	 */
	public static String getNodevalue(org.dom4j.Node root,String path) {
		String val = "";
		org.dom4j.Node node = root
				.selectSingleNode(path);
		if (node != null) {
			val = node.getStringValue();
		}
		return val;
	}
	
	/**
	 * 获取节点属性
	 * @param root
	 * @param path
	 * @param attrName
	 * @return
	 */
	public static String getNodeAttrVal(Element root,String path, String attrName) {
		String attrVal ="";
		Node node = root.selectSingleNode(path);
		if (null != node) {
			Element element  = (Element)node;
			Attribute attr = element.attribute(attrName);
			if (null!=attr && StringUtils.isNotBlank(attr.getValue())) {
				attrVal = attr.getValue();
			}
		}
		return attrVal;
	}
	
	/**
	 * 获取节点值
	 * @param root
	 * @param path
	 * @return
	 */
	public static String getNodeVal(Element root,String path) {
		String val ="";
		Node node = root.selectSingleNode(path);
		if (null != node && StringUtils.isNotBlank(node.getStringValue())) {
			val  = node.getStringValue();
		}
		return val;
	}
	
	public static void main(String[] args){
		String testXml = "<root><test><a>aaa</a></test><ling><b>bbb</b></ling></root>";
		try {
			Document doc = parseText(testXml);
			System.out.println(((Element)doc.getRootElement().elements().get(0)).asXML());
			System.out.println(((Element)doc.getRootElement().elements().get(1)).asXML());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}

class DefaultXMLEventFilter implements javax.xml.stream.EventFilter {
	public boolean accept(XMLStreamReader reader) {
		System.out.println("event :" + reader.getEventType());
		if (!reader.isStartElement() && !reader.isEndElement()) {

			return false;
		} else {
			if (reader.getEventType() == XMLEvent.COMMENT) {
				System.out.println("commment :" + reader.getText());
				return false;
			}
			return true;
		}
	}

	@Override
	public boolean accept(XMLEvent event) {
		return !(event.getEventType() == XMLEvent.COMMENT);
	}
}
